# -*- coding: utf-8 -*-
# @Time    :2020/5/28 20:12
# @Author  :lyq
from django import forms
from .models import *


class RegisterForm(forms.Form):
    account = forms.EmailField(max_length=20,
                               error_messages={'max_length': '长度不能超过20',
                                               'required': '用户名不能为空'})
    password = forms.CharField(max_length=32, min_length=32,
                               error_messages={'max_lenght': '长度不能超过32',
                                               'min_lenght': '长度不能小于32',
                                               'required': '密码不能为空'})

    def clean(self):
        forms.Form.clean(self)
        email = self.cleaned_data.get('account', '')
        if email:
            import re
            if not re.match(r'^[a-zA-Z0-9_-]{6,}@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$',email):
                self.errors['account'] = ['邮箱格式不正确']
            if User.is_exist(email):
                self.errors['account'] = ['当前账号已存在']
